package com.baizhi.springmvc.controller;

import com.alibaba.fastjson.JSON;

import com.baizhi.springmvc.entity.Book;
import com.baizhi.springmvc.entity.Cart;
import com.baizhi.springmvc.entity.Item;
import com.baizhi.springmvc.entity.User;
import com.baizhi.springmvc.service.BookService;
import com.baizhi.springmvc.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Controller
public class CartController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/addCart")
    @ResponseBody
    public void addCart(Integer bookid, Integer count, HttpSession session) {
    User user = (User) session.getAttribute("user");
    Jedis jedis = RedisUtil.getJedis();
    String s = jedis.get(user.getUser_id()+"");
        Cart cart=null;
        if (s == null) {
            cart = new Cart();
        } else {
            cart = JSON.parseObject(s, Cart.class);
        }
        HashMap<Integer, Item> map = cart.getMap();
        Item item = map.get(bookid);

        if (item == null) {
            item = new Item();
            Book book = bookService.selectOne(bookid);
            item.setBook(book);
            if (count == null) {
                count=1;
            }
            item.setCount(count);
            map.put(bookid, item);
        } else {
            item.setCount(item.getCount()+1);
        }
        Collection<Item> values = map.values();
        Double sum=0.0;
        Double save=0.0;
        for (Item i : values) {
            sum+=i.getBook().getDangPrice()*i.getCount();
            save+=i.getBook().getBook_price()*i.getCount();
        }
        cart.setTotalPrice(sum);
        cart.setSavePrice(save-sum);
        jedis.set(user.getUser_id()+"", JSON.toJSONString(cart));
        jedis.close();

    }
@RequestMapping("/showCart")
    public String showCart(HttpSession session,Map map) {
        User user = (User) session.getAttribute("user");
        Jedis jedis = RedisUtil.getJedis();
        String s = jedis.get(user.getUser_id()+"");
       Cart cart = JSON.parseObject(s, Cart.class);
        map.put("cart", cart);
        jedis.close();
        return "forward:/cart/cart_list.jsp";
    }
@RequestMapping("/orderInfo")
    public String orderInfo(HttpSession session,Map map) {
        User user = (User) session.getAttribute("user");
        Jedis jedis = RedisUtil.getJedis();
        String s = jedis.get(user.getUser_id()+"");
        Cart cart = JSON.parseObject(s, Cart.class);
        map.put("cart", cart);
        jedis.close();
        return "forward:/order/order_info.jsp";
    }
@RequestMapping("/changeCart")
    public String changeCart(Integer bookid,Integer count,HttpSession session) {
        User user = (User) session.getAttribute("user");
        Jedis jedis = RedisUtil.getJedis();
        String s = jedis.get(user.getUser_id()+"");
        Cart cart = JSON.parseObject(s, Cart.class);
        HashMap<Integer, Item> map = cart.getMap();
        Item item = map.get(bookid);
        if (count == null) {
            count=1;
        }
        item.setCount(count);
        map.put(bookid, item);
        Collection<Item> values = map.values();
        Double sum=0.0;
        Double save=0.0;
        for (Item i : values) {
            sum += i.getBook().getDangPrice() * i.getCount();
            save += i.getBook().getBook_price() * i.getCount();
        }
        cart.setTotalPrice(sum);
        cart.setSavePrice(save-sum);
        jedis.set(user.getUser_id()+"", JSON.toJSONString(cart));

        return "redirect:/showCart.do";
    }
@RequestMapping("/removeCart")
    public String removeCart(Integer bookid,Integer count,HttpSession session) {
        User user = (User) session.getAttribute("user");
        Jedis jedis = RedisUtil.getJedis();
        String s = jedis.get(user.getUser_id()+"");
        Cart cart = JSON.parseObject(s, Cart.class);
        HashMap<Integer, Item> map = cart.getMap();
        map.remove(bookid);
        HashMap<Integer, Item> removeMap = cart.getRemoveMap();
        Item item = new Item();
        Book book = bookService.selectOne(bookid);
        item.setBook(book);
        if (count == null) {
            count=1;
        }
        item.setCount(count);
        removeMap.put(bookid,item);
        Collection<Item> values = map.values();
        Double sum=0.0;
        Double save=0.0;
        for (Item ma:values) {
            sum+=ma.getCount()*ma.getBook().getDangPrice();
            save+=ma.getCount()*ma.getBook().getBook_price();
        }
        cart.setTotalPrice(sum);
        cart.setSavePrice(save-sum);
        jedis.set(user.getUser_id()+"", JSON.toJSONString(cart));

        return "redirect:/showCart.do";
    }
@RequestMapping("/recoverCart")
    public String recoverCart(Integer bookid,Integer count,HttpSession session) {
        User user = (User) session.getAttribute("user");
        Jedis jedis = RedisUtil.getJedis();
        String s = jedis.get(user.getUser_id()+"");
        Cart cart = JSON.parseObject(s, Cart.class);
        HashMap<Integer, Item> removeMap = cart.getRemoveMap();
        removeMap.remove(bookid);
        HashMap<Integer, Item> map = cart.getMap();
        Item item = new Item();
        Book book = bookService.selectOne(bookid);
        item.setBook(book);
        if (count == null) {
            count=1;
        }
        item.setCount(count);
        map.put(bookid,item);
        Collection<Item> values = map.values();
        Double sum=0.0;
        Double save=0.0;
        for (Item ma : values) {
            sum+=ma.getBook().getDangPrice()*ma.getCount();
            save+=ma.getBook().getBook_price()*ma.getCount();
        }
        cart.setTotalPrice(sum);
        cart.setSavePrice(save-sum);
        jedis.set(user.getUser_id()+"", JSON.toJSONString(cart));
        return "redirect:/showCart.do";
    }
}
